//
//  EAppLogoView.m
//  AppRanking
//
//  Created by Gang.Wang on 12-11-9.
//  Copyright (c) 2012年 Gang.Wang. All rights reserved.
//

#import "EAppLogoView.h"
#import "mcf.h"

#import "CERoundProgressView.h"
#import "EAppImageDowloadRecv.h"

#import "DCRoundSwitch.h"

@interface EAppLogoView ()<YDownloadRequestDelegate>
{
    CERoundProgressView * progressView_;
    UIImageView * appLogoView_;
    
    EAppImageDowloadRecv * recvData_;
    DownloadRequest * request_;
    
    DCRoundSwitch * platfromSwitch_;
    BOOL currentIsiPhone;
    
}

@property (nonatomic, strong) CERoundProgressView * progressView;
@property (nonatomic, strong) UIImageView * appLogoView;

@property (nonatomic, strong) EAppImageDowloadRecv * recvData;
@property (nonatomic, strong) DownloadRequest * request;

@property (nonatomic, retain) DCRoundSwitch * platfromSwitch;

- (void) showDataToView;

@end



@implementation EAppLogoView


@synthesize progressView = progressView_;
@synthesize appLogoView = appLogoView_;

@synthesize appData = appData_;

@synthesize appId = appId_;

@synthesize recvData = recvData_;
@synthesize request = request_;

@synthesize delegate = delegate_;
@synthesize platfromSwitch = platfromSwitch_;

- (void) dealloc
{
    YRelease(progressView_)
    YRelease(appLogoView_)
    
    YRelease(appId_)
    YRelease(appData_)
    
    YRelease(recvData_)
    YRelease(request_)
    
    YRelease(platfromSwitch_)
    
    [super dealloc];
}


- (id) init
{
    self = [super init];
    if (self)
    {
        [self privateInit];
    }
    
    return self;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self privateInit];
    }
    return self;
}

- (void) setFrame:(CGRect)frame
{
    [super setFrame:frame];
    [self reAdjustLayout];
}


- (void) privateInit
{
    if (self.bInited)
    {
        return;
    }
    
    CERoundProgressView * temp_progressView = [[CERoundProgressView alloc] init];
    
    self.progressView = temp_progressView;
    YRelease(temp_progressView)
    [self addSubview:self.progressView];
    
    
    UIImageView * temp_imageView = [[UIImageView alloc] init];
    
    self.appLogoView = temp_imageView;
    YRelease(temp_imageView)
    [self addSubview:self.appLogoView];
    
    [self.appLogoView  setBackgroundColor:COLOR_CLEAR];
    
    DCRoundSwitch * temp_switch = [[DCRoundSwitch alloc] init];
    
    [temp_switch setOffText:@"iPad"];
    [temp_switch setOnText:@"iPhone"];
    
    [temp_switch setOn:YES];
    [temp_switch setOffTintColor:COLOR_ORANGE];
    
    [temp_switch addTarget:self action:@selector(platfromSwitched) forControlEvents:UIControlEventValueChanged];
    
    self.platfromSwitch = temp_switch;
    YRelease(temp_switch);
    [self addSubview:self.platfromSwitch];
    
    
    [self reAdjustLayout];
    
    self.bInited = YES;
}

- (void) reAdjustLayout
{
    int width = self.width > self.height ? self.height : self.width / 3 * 2;
    
    width -= 5;
    
    [self.progressView setFrame:CGRectMake((self.width - width) / 2, (self.height - width) / 2, width, width)];
    [self.appLogoView setFrame:CGRectMake((self.width - width) / 2, (self.height - width) / 2, width, width)];
    
    if (self.appData.isForiPhoneAndiPad)
    {
        [self.platfromSwitch setFrame:CGRectMake(20, self.height - 25, self.width - 40, 25)];
    }
}

- (void) platfromSwitched
{
    currentIsiPhone = self.platfromSwitch.isOn;
    self.appData.currentRequestPlatfromIsiPhone = currentIsiPhone;
}


- (void) setAppData:(EAppInfo *)appDataValue
{
    YRelease(appData_)
    appData_ = [appDataValue retain];
    
    [self showDataToView];
}

- (void) showDataToView
{
    if (!self.appData)
    {
        return;
    }
    
    EAppImageDowloadRecv * temp_recvData = [[EAppImageDowloadRecv alloc] init];
    
    [temp_recvData setFileName:[YPath fileName:self.appData.artworkUrl]];
    
    self.recvData = temp_recvData;
    YRelease(temp_recvData);
    
    if (self.recvData.bCached)
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
            // in thread
            sleep(0.52f);
            UIImage * image = [UIImage decompressedImage:[self.recvData data]];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                // main thread
                if (image)
                {
                    [self.appLogoView setImage:image];
                }
            });
        });
        return;
    }
    
    
    DownloadRequest * temp_requset = [[DownloadRequest alloc] init];
    
    YDownloadObjectModel * downModel = [[YDownloadObjectModel alloc] init];
    downModel.url = self.appData.artworkUrl;
    temp_requset.downloadModel = downModel;
    YRelease(downModel)
    
    [temp_requset setDelegate:self];
    self.request = temp_requset;
    YRelease(temp_requset)
    
    [self.request setRecvData:self.recvData];
    
    DELEGATE_CALLBACK(need2Download:, self.request)
    
}


- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    DELEGATE_CALLBACK(appClicked:, self.appData);
}


#pragma mark - YDownloadRequestDelegate
- (void) didTaskReceiveDataFinish   : (NSData *) theData    request:(DownloadRequest*) request
{
    //
    [self.appLogoView setImage:[UIImage imageWithData:theData]];
}

- (void) didTaskReceiveFilename     : (NSString *) aName    request:(DownloadRequest*) request
{
    [self.recvData setFileName:aName];
}

- (void) taskDataDownloadFailed     : (NSError *) error   request:(DownloadRequest*) request
{
    
}

- (void) taskDataDownloadAtPercent  : (NSNumber *) aPercent request:(DownloadRequest*) request
{
    
}


@end
